package com.qfedu.edu.queuemonitorservice.config;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.qfedu.edu.queuemonitorservice.job.MySimpleJob;
import com.qfedu.edu.queuemonitorservice.properties.ElasticJobProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * @author xiaobobo
 * @title: ElasticJobConfig
 * @projectName cd-java-fy-2401-springboot-parent
 * @description: 配置ElasticJob
 * @date 2024/10/31  9:37
 */
@SpringBootConfiguration
@EnableConfigurationProperties(ElasticJobProperties.class)
public class ElasticJobConfig {

    @Autowired
    private MySimpleJob mySimpleJob;

    @Bean
    public LiteJobConfiguration liteJobConfiguration(ElasticJobProperties elasticJobProperties){
        JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder(elasticJobProperties.getTaskJobName(), elasticJobProperties.getCron(), elasticJobProperties.getShardingTotalCount())
                .build();
        //将核心配置应用到具体的实例
        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, mySimpleJob.getClass().getName());
        //构建咋们的调度器
        LiteJobConfiguration build = LiteJobConfiguration.newBuilder(simpleJobConfiguration).build();
        return build;
    }

    @Bean(initMethod = "init")
    public JobScheduler jobScheduler(ZookeeperRegistryCenter zookeeperRegistryCenter,LiteJobConfiguration liteJobConfiguration){
        SpringJobScheduler springJobScheduler = new SpringJobScheduler(mySimpleJob, zookeeperRegistryCenter, liteJobConfiguration);
        return springJobScheduler;
    }

}
